package com.fuli.app;

import com.fuli.app.common.fuliRealm;
import com.fuli.app.filter.AnyRolesAuthorizationFilter;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.DelegatingFilterProxy;

import javax.servlet.Filter;
import java.util.LinkedHashMap;
import java.util.Map;

//@Configuration
public class ShiroConfig {
    @Autowired
    private AnyRolesAuthorizationFilter anyRolesAuthorizationFilter;

    @Bean
    public fuliRealm fuliRealm() {
        return new fuliRealm();
    }

    @Bean
    public DefaultWebSecurityManager securityManager(fuliRealm fuliRealm) {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(fuliRealm);
        return securityManager;
    }

    @Bean
    public FilterRegistrationBean delegatingFilterProxy() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        DelegatingFilterProxy proxy = new DelegatingFilterProxy();
        proxy.setTargetFilterLifecycle(true);
        proxy.setTargetBeanName("shiroFilterFactoryBean");
        filterRegistrationBean.setFilter(proxy);
        return filterRegistrationBean;
    }

    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        /*添加自定义拦截器*/
        Map<String, Filter> filters = shiroFilterFactoryBean.getFilters();
        filters.put("anyRoles", anyRolesAuthorizationFilter);
        shiroFilterFactoryBean.setFilters(filters);

        shiroFilterFactoryBean.setSecurityManager(securityManager);

        shiroFilterFactoryBean.setLoginUrl("/login");
        shiroFilterFactoryBean.setSuccessUrl("/index");
        shiroFilterFactoryBean.setUnauthorizedUrl("/403");

        Map<String, String> mapFilter = new LinkedHashMap<>();

        mapFilter.put("/curriculumCategory/list", "anon");
        mapFilter.put("/curriculumCategory/page", "anon");
        mapFilter.put("/curriculumCategory/listAll", "anon");
        mapFilter.put("/login", "anon");
        mapFilter.put("/logout", "authc");
        mapFilter.put("/Message/add", "anon");
        mapFilter.put("/Message/uuid", "anon");
        mapFilter.put("/review/add", "anon");
        mapFilter.put("/signIn/add", "anon");
        mapFilter.put("/work/add", "anon");
        mapFilter.put("/review/delete/**", "anon");
        mapFilter.put("/signIn/delete/**", "anon");
        mapFilter.put("/work/delete/**", "anon");
        mapFilter.put("/user/existAccount/**", "anon");



        mapFilter.put("/*/get/**", "anon");
        mapFilter.put("/*/get**/**", "anon");
        mapFilter.put("/*/pageQuery", "anon");
        mapFilter.put("/*/openId", "anon");
        mapFilter.put("/**", "authc");

        shiroFilterFactoryBean.setFilterChainDefinitionMap(mapFilter);
        System.out.println("注入权限...");
        return shiroFilterFactoryBean;
    }
}
